<?php

namespace RjYst\Category\Services;

use Illuminate\Support\Facades\File;

class EnvUpdater
{
    /**
     * 添加环境变量到 .env 文件
     */
    public static function addEnvVariables()
    {
        $envPath = base_path('.env');

        if (!File::exists($envPath)) {
            return false;
        }

        $envContent = File::get($envPath);
        $newVariables = [];

        // 检查并添加新的环境变量
        $variables = [
            'DB_DATABASE_RJ' => 'your_second_database',
            'DB_USERNAME_RJ' => 'your_username',
            'DB_PASSWORD_RJ' => 'your_password',
        ];

        foreach ($variables as $key => $defaultValue) {
            if (strpos($envContent, $key) === false) {
                $newVariables[] = "{$key}={$defaultValue}";
            }
        }

        if (!empty($newVariables)) {
            // 添加注释和新变量
            $envContent .= "\n# RjYst Category Package - 第二数据库配置\n";
            $envContent .= implode("\n", $newVariables) . "\n";

            File::put($envPath, $envContent);
            return true;
        }

        return false;
    }

    /**
     * 检查环境变量是否存在
     */
    public static function hasEnvVariables()
    {
        $envPath = base_path('.env');

        if (!File::exists($envPath)) {
            return false;
        }

        $envContent = File::get($envPath);
        return strpos($envContent, 'DB_DATABASE_RJ') !== false;
    }
}
